//
//  LHRecommendCityHomePageCollectionView.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-15.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommendCityHomePageCollectionView.h"
#import "LHRecommendCityHomePageCollectionViewCell.h"
#import "LHNetworkHandle.h"
#import "LHRecommendCityHomePageModel.h"

@interface LHRecommendCityHomePageCollectionView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) UICollectionView *popularSight;
@property (nonatomic, retain) NSMutableArray *sightArr;

@end

@implementation LHRecommendCityHomePageCollectionView

- (void)dealloc
{
    [_flowLayout release];
    [_popularSight release];
    [_sightArr release];
    [super dealloc];
    
}


/**
 *  初始化
 *
 *  @return self
 */

- (instancetype)initWithFrame:(CGRect)frame cityId:(NSString *)cityId
{
    if (self = [super initWithFrame:frame]) {
        
        
        
        [self getDataWithCityId:cityId];
    }
    
    return self;
}

/**
 *  创建热门目的地collectionView
 */
- (void)createPopularSightCollectionView
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    _flowLayout.itemSize = CGSizeMake((self.frame.size.width - 45) / 3, (self.frame.size.width - 45) / 3);
    
    _flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 5, 10);
    
    
    // 创建collectionView
    self.popularSight = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height + 14) collectionViewLayout:_flowLayout];
    
    _popularSight.backgroundColor = [UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
    
    _popularSight.delegate = self;
    _popularSight.dataSource = self;

    
    [self addSubview:_popularSight];
    
    [_popularSight release];
    
    [_popularSight registerClass:[LHRecommendCityHomePageCollectionViewCell class] forCellWithReuseIdentifier:@"rr"];
    
}

#pragma mark - collectionView协议方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 8;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LHRecommendCityHomePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"rr" forIndexPath:indexPath];
    
    cell.popularSight = _sightArr[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate jumpToSightDetailViewWithIndexPath:indexPath sightArr:_sightArr];
}

/**
 *  获取网络数据
 */
- (void)getDataWithCityId:(NSString *)cityId
{
    NSString *newId = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/3/%@/pois/sights/?start=0&count=20&sort=default&shift=false&latitude=38.88266356313796&longitude=121.53966915009306", cityId];
    
    
    [LHNetworkHandle getDataWithURL:newId completion:^(id result) {
        
        self.sightArr = [NSMutableArray array];
        NSArray *arr = [result objectForKey:@"items"];
        
        for (NSDictionary *tempDic in arr) {
            
            LHRecommendCityHomePageModel *reCityHomePage = [[LHRecommendCityHomePageModel alloc] initWithDic:tempDic];
            
            [_sightArr addObject:reCityHomePage];
            
            [reCityHomePage release];
            
        }
        
        [self createPopularSightCollectionView];
        
    }];
    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
